package com.wlw.admin.openeye.ui.activity

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Typeface
import android.os.Build
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import com.orhanobut.logger.Logger
import com.wlw.admin.openeye.MyApplication
import com.wlw.admin.openeye.R
import com.wlw.admin.openeye.base.BaseActivity
import com.wlw.admin.openeye.showToast
import com.wlw.admin.openeye.utils.AppUtils
import kotlinx.android.synthetic.main.activity_splash.*
import me.weyye.hipermission.HiPermission
import me.weyye.hipermission.PermissionCallback
import me.weyye.hipermission.PermissionItem

class SplashActivity : BaseActivity() {
    private var textTypeface: Typeface? = null
    private var descTypeface: Typeface? = null
    private var alphaAnimation: AlphaAnimation? = null
    private val mPermissions by lazy {
        listOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE)
    }

    init {
        textTypeface = Typeface.createFromAsset(MyApplication.context.assets, "fonts/Lobster-1.4.otf")
        descTypeface = Typeface.createFromAsset(MyApplication.context.assets, "fonts/FZLanTingHeiS-L-GB-Regular.TTF")
    }

    override fun layoutId(): Int = R.layout.activity_splash
    @SuppressLint("SetTextI18n")
    override fun initData() {
        tv_app_name.typeface = textTypeface
        tv_splash_desc.typeface = descTypeface
        tv_version_name.text = "V${AppUtils.getVerName(this)}"
        alphaAnimation = AlphaAnimation(0.3f, 1.0f)
        alphaAnimation?.duration = 2000
        alphaAnimation?.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(p0: Animation?) {
            }

            override fun onAnimationEnd(p0: Animation?) {
                redirectTo()
            }

            override fun onAnimationStart(p0: Animation?) {
            }
        })
        checkPermission()
    }

    private fun redirectTo() {
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }

    private fun checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(mPermissions.toTypedArray(), 100)
        } else {
            layout_splash.startAnimation(alphaAnimation)
        }
    }

    private fun checkPermission(string: String) {
        val permissionItems = ArrayList<PermissionItem>()
        permissionItems.add(PermissionItem(Manifest.permission.READ_PHONE_STATE, "手机状态", R.drawable.permission_ic_phone))
        permissionItems.add(PermissionItem(Manifest.permission.WRITE_EXTERNAL_STORAGE, "存储空间", R.drawable.permission_ic_storage))
        HiPermission.create(this)
                .title("尊敬的用户")
                .msg("为了能正常使用，请开启这些权限")
                .permissions(permissionItems)
                .style(R.style.PermissionDefaultNormalStyle)
                .animStyle(R.style.PermissionAnimFade)
                .checkMutiPermission(object : PermissionCallback {
                    override fun onFinish() {
                        showToast("初始化完毕！")
                        layout_splash.startAnimation(alphaAnimation)

                    }

                    override fun onDeny(permission: String?, position: Int) {
                        Logger.i("permission_onDeny")
                    }

                    override fun onGuarantee(permission: String?, position: Int) {
                        Logger.i("permission_onGuarantee")
                    }

                    override fun onClose() {

                        Logger.i("permission_onClose")
                        showToast("用户关闭了权限")
                    }
                })
    }

    override fun initView() {
    }

    override fun start() {
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 100) {
            layout_splash.startAnimation(alphaAnimation)
//            val size = permissions.size
//            for (i in 0..size) {
//                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
//
//                } else {
//                    //权限拒绝
//                    showToast("权限拒绝")
//                }
//            }
        }
    }
}
